/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.spoxcy.util;

/**
 *
 * @author Denis.Duan
 * @date Apr 20, 2009
 */
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import sun.misc.BASE64Encoder;

public final class PasswordService {

    private static PasswordService instance;

    private PasswordService() {
    }

    public synchronized String encrypt(String plaintext) {
        MessageDigest md = null;
        try {
            md = MessageDigest.getInstance("SHA");
        } catch (NoSuchAlgorithmException e) {
        }
        try {
            md.update(plaintext.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e) {
        }

        byte raw[] = md.digest();
        String hash = (new BASE64Encoder()).encode(raw);
        return hash;
    }

    public static synchronized PasswordService getInstance(){
        if (instance == null) {
            instance = new PasswordService();
        }
        return instance;
    }

    public static void main(String[] args) {
        String name = "denis";
        String testName = "denis";
        System.out.println(PasswordService.getInstance().encrypt(name));
        System.out.println(PasswordService.getInstance().encrypt(testName));

    }
}

